// pages/address/my-address/my-address.js
//pages/address/my-address/my-address?intent=select//选择,实现onResultAddressSelected(address)方法
//pages/address/my-address/my-address?intent=edit//编辑
var httpUtil = require('../../../utils/httpUtil.js')
var uiUtil = require('../../../utils/uiUtil.js')
var util = require('../../../utils/util.js')
var emptyUtil = require('../../templates/empty/empty.js')
var that

const INTENT_SELECT = "select"
const INTENT_EDIT = "edit"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    intent: INTENT_SELECT, //意图

    addressList: null, //
    emptyParam: {
      emptyStatus: 0
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    try {
      var intent = options.intent || INTENT_SELECT
      this.setData({
        intent: intent
      })
    } catch (e) {}

    this.requestAddressList()
  },
  onShow: function() {
    var addressList = this.data.addressList
    if (addressList && addressList.length != 0) {
      emptyUtil.hideView()
    }
  },
  onShareAppMessage: function (res) {
    return util.handleShareApp()
  },

  /**
   * 条目点击
   */
  onItemClick(e) {
    if (INTENT_SELECT == this.data.intent) {
      var index = e.currentTarget.dataset.index;
      var address = that.data.addressList[index]
      if (address.status == 1) {
        try {
          var pages = getCurrentPages()
          var lastPage = pages[pages.length - 2]
          lastPage.onResultAddressSelected(address)
          wx.navigateBack({})
        } catch (e) {
          console.log(e)
        }
      } else {
        uiUtil.showToast("请选择其他地址")
      }
    }
  },
  /**
   * 删除点击
   */
  onDelClick(e) {
    var index = e.currentTarget.dataset.index;
    var addressId = e.currentTarget.dataset.addressid;

    uiUtil.showLoading("请稍候..")
    httpUtil.PostRequest("del_take_adress", {
      addressId: addressId
    }, {
      complete() {},
      success(res) {
        uiUtil.showToast("删除成功")
        var addressList = that.data.addressList
        addressList.splice(index, 1);
        that.setData({
          addressList: addressList
        })
        if (addressList.length == 0) {
          emptyUtil.showEmptyView()
        }
      },
      fail(code, res) {
        uiUtil.showToast(res || "删除失败")
      }
    })

  },

  /**
   * 编辑
   */
  onEditClick(e) {
    var index = e.currentTarget.dataset.index;
    var address = this.data.addressList[index]
    wx.navigateTo({
      url: '../add-address/add-address?addressInfo=' + encodeURIComponent(JSON.stringify(address)),
    })
  },

  /**
   * 设为默认
   */
  onSetDefaultClick(e) {
    var index = e.currentTarget.dataset.index;
    var address = this.data.addressList[index]

    uiUtil.showLoading("请稍候..")
    httpUtil.PostRequest("default_take_adress", {
      addressId: address.addressId
    }, {
      complete() {},
      success(res) {
        uiUtil.showToast("设置成功")
        var addressList = that.data.addressList
        var length = addressList.length
        for (var i = 0; i < length; i++) {
          var item = addressList[i]
          if (i == index) { //设置默认
            item.isDefaultAddress = '1'
          } else {
            item.isDefaultAddress = '0'
          }
        }
        that.setData({
          addressList: addressList
        })

      },
      fail(code, res) {
        uiUtil.showToast(res || "设置失败,请稍候重试.")
      }
    })
  },

  /**
   * 结果返回-新增收货地址
   */
  onResultAddAddress(addressInfo) {
    if (!addressInfo) {
      return
    }
    var addressList = this.data.addressList || []
    addressList.push(addressInfo)
    this.setData({
      addressList: addressList
    })
  },
  /**
   * 结果返回-收货地址变更
   */
  onResultEditAddress(addressInfo) {
    if (!addressInfo) {
      return
    }
    var addressList = this.data.addressList || []
    for (var i = 0; i < addressList.length; i++) {
      var oldItem = addressList[i]
      if (oldItem.addressId == addressInfo.addressId) {
        addressList.splice(i, 1, addressInfo)
        that.setData({
          addressList: addressList
        })
        break;
      }
    }
  },


  /**
   * 获取收货地址列表
   */
  requestAddressList() {
    uiUtil.showLoading("加载中..")
    httpUtil.PostRequest("take_adress_list", {}, {
      complete() {
        wx.hideLoading()
      },
      success(res) {
        if (!res || res.length == 0) {
          emptyUtil.showEmptyView()
        } else {
          that.setData({
            addressList: res
          })
        }
      },
      fail(code, res) {
        var text = res || "获取收货地址失败"
        emptyUtil.showFailView(text)
      }
    })
  },
})